﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Administration/Views/Shared/Administration.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MotorcycleStore.DomainModel.Manufacturer>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

	<script type="text/javascript">

		$(function () {
			$("#manufacturer-dialog").dialog({
				autoOpen: false,
				buttons: {
					'Ok': function () {
						if ($("#manufacturer-form").valid()) {
							$.ajax({
								data: $("#manufacturer-form").serialize(),
								dataType: "html",
								error: handle_ajax_err,
								success: function (data) {
									$("#manufacturer-list").html(data);
									register_list_clicks();
								},
								type: "POST",
								url: '<%: Url.Action("Create") %>'
							});

							$(this).dialog('close');
						}
					}
				},
				modal: true
			});

			$("#open-manufacturer-dialog").button({
				icons: {
					primary: 'ui-icon-newwin'
				}
			}).click(function () {
				$("#manufacturer-dialog").dialog('open');
				return false;
			});

			register_list_clicks();

		});

		function register_list_clicks() {
			$("a.manufacturer-delete").button({
				icons: {
					primary: 'ui-icon-trash'
				}
			}).click(function () {
				var row = $(this).parents("tr");
				if (confirm("Are you sure you wish to delete " + $(this).parent().next().text() + " and associated motorcycles?")) {
					$.ajax({
						error: handle_ajax_err,
						success: function (data) {
							row.fadeOut();
							row.remove();
						},
						type: "POST",
						url: $(this).attr("href")
					});
				}
				return false;
			});

			$("a.manufacturer-edit").button({
				icons: {
					primary: 'ui-icon-wrench'
				}
			});

			$("a.manufacturer-detail").button({
				icons: {
					primary: 'ui-icon-tag'
				}
			});

			$("#manufacturer-form").validate({
				rules: {
					Name: {
						required: true,
						remote: {
							url: '<%: Url.Action("IsUnique") %>',
							data: {
								name: function () {
									return $("#Name").val();
								}
							}
						}
					},
					Website: {
						required: true,
						url: true
					}
				}
			});
		}

		function handle_ajax_err(xhr, status, err) {
			alert("ERROR:\n\n" + xhr.responseText);
		}

	</script>

    <h2>Manufacturer Index</h2>

	<div id="manufacturer-list">
		<% Html.RenderPartial("ManufacturerList"); %>
	</div>

    <p>
        <%: Html.ActionLink("New Manufacturer", "Create", null, new { id = "open-manufacturer-dialog" })%>
    </p>

	<div id="manufacturer-dialog" title="New Manufacturer" class="ui-helper-hidden">
		<form id="manufacturer-form" action="<%: Url.Action("Create") %>">
			<% Html.RenderAction("ManufacturerForm"); %>
		</form>
	</div>

</asp:Content>

